<?php 
/**
 *  Por Federico Pisarello - SisDev Software (c) 2012 - Buenos Aires, Argentina.
 * 	fpisarello@sisdevsoft.com
 */

require("includes/funciones.inc.php");

session_start();
if(!isset($_SESSION['userName']))
	exit();

function searchABMTypeFoods()
{
	require("includes/constants.php");
	require("includes/datatables_db/Datatables.php");
	
	$datatables = new Datatables();
	
	$datatables
			->select('idTypeFood, typeDescription')
			->from('typefood')
			->where("typefood.state <> '".$stateErase."'")
			->join('typemenufoods','typefood.idMenuFood = typemenufoods.idTypeMenuFood')
			->select('description')
			->edit_column('description','$1','getDescriptionRes(description)')
			->join('restaurant','restaurant.idRestaurant = typefood.idRestaurant')
			->select('businessName');
	
	if($_SESSION['userType'] != $idTypeAdministrator)
		$datatables->where("typefood.idRestaurant = ".$_SESSION['userRestaurant']);

	return $datatables->generate();
}

function addOrEdit()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;

	$DB->StartTrans(); // start transaction
	
	if(!isset($_REQUEST["idTypeFood"])){
					
		$strSQL = "INSERT INTO typefood ( ";
		$strSQL .= "	typeDescription, ";
		$strSQL .= "	state, ";
		$strSQL .= "	idMenuFood, ";
		$strSQL .= "	idRestaurant ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["typeFoodsDescription"], "UTF-8")."', ";
		$strSQL .= "	'".$stateAdd."', ";
		$strSQL .= "	".$_REQUEST["cmbTypeFoods"].", ";
		if($_SESSION['userType'] == $idTypeAdministrator)
			$strSQL .= "	'".$_REQUEST["cmbRestaurant"]."' ";
		else
			$strSQL .= "	'".$_SESSION['userRestaurant']."' ";
		$strSQL .= "	)";
	}
	else{
		$strSQL = "UPDATE typefood SET ";
		$strSQL .= "	typeDescription = '".mb_strtoupper($_REQUEST["typeFoodsDescription"], "UTF-8")."', ";
		$strSQL .= "	state = '".$stateModify."', ";
		$strSQL .= "	idMenuFood = ".$_REQUEST["cmbTypeFoods"].", ";
		if($_SESSION['userType'] == $idTypeAdministrator)
			$strSQL .= "	idRestaurant = ".$_REQUEST["cmbRestaurant"];
		else
			$strSQL .= "	idRestaurant = ".$_SESSION['userRestaurant'];
		$strSQL .= " WHERE idTypeFood = ". $_REQUEST["idTypeFood"];
	}
	
	$DB->Execute($strSQL);
	
	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorGrabar'];
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Common.OkGrabar'];
	}	
	return json_encode($arrJSON);
}

function del()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$DB->StartTrans(); // start transaction
	$DB->Execute("UPDATE typefood SET state='$stateErase' where idTypeFood=" . $_REQUEST["idTypeFood"]); // execute query

	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorEliminar'];
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Common.OkEliminar'];
	}	
	return json_encode($arrJSON);
}

function editTypefood()
{
	require("includes/constants.php");
		
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	if($_SESSION['userType'] == $idTypeAdministrator)
		$rsTypeFood = $DB->Execute("SELECT * FROM typefood WHERE state <> '$stateErase' AND idTypeFood=" . $_REQUEST["idTypeFood"]); // execute query
	else
		$rsTypeFood = $DB->Execute("SELECT * FROM typefood WHERE state <> '$stateErase' AND idRestaurant = ".$_SESSION['userRestaurant'] ." AND idTypeFood=" . $_REQUEST["idTypeFood"]); // execute query
						
	if(!$rsTypeFood->EOF){
		$joTypeFood->data['description'] = $rsTypeFood->fields("typeDescription");
		$joTypeFood->data['idMenuFood'] = $rsTypeFood->fields("idMenuFood");
		$joTypeFood->data['idRestaurant'] = $rsTypeFood->fields("idRestaurant");
		
		return json_encode($joTypeFood->data);
	}
	else
		return json_encode('');
}

function getRestaurants()
{
	require("includes/selectLanguage.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsRestaurant = $DB->Execute("SELECT * FROM restaurant");
	
	$i=0;
	while(!$rsRestaurant->EOF){
		$arrRestaurant->data[$i]['idRestaurant'] = $rsRestaurant->fields(0);
		$arrRestaurant->data[$i++]['businessName'] = $rsRestaurant->fields(1);
		$rsRestaurant->MoveNext();
	}
	
	return json_encode($arrRestaurant->data);
}

function getMenuFoods()
{
	require("includes/selectLanguage.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsTypeMenuFoods = $DB->Execute("SELECT * FROM typemenufoods");
	
	$i=0;
	while(!$rsTypeMenuFoods->EOF){
		$arrTypeMenuFoods->data[$i]['idTypeMenuFood'] = $rsTypeMenuFoods->fields(0);
		$arrTypeMenuFoods->data[$i++]['description'] = $a_languages[$rsTypeMenuFoods->fields(1)];
		$rsTypeMenuFoods->MoveNext();
	}
	
	return json_encode($arrTypeMenuFoods->data);
}

switch($_REQUEST['actionOfForm'])
{
	case "EDIT":
		echo editTypefood();
		break;
	case "DELETE":
		echo del();
		break;
	case "search":
		echo searchABMTypeFoods();
		break;
	case "addOrEdit":
		echo addOrEdit();
		break;
	case "getRestaurants":
		echo getRestaurants();
		break;
	case "getMenuFoods":
		echo getMenuFoods();
		break;
}
?>
